﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Write.Application.Common.Leave;
using MediatR;

namespace B.S.BasicData.Write.Application.Handler.Leave
{
    public class LeaveCommonHandle:IRequestHandler<LeaveCommon,ApiResult<int>>
    {
        private readonly IBaseRepository<LeaveModel> LeaveDal;

        public LeaveCommonHandle(IBaseRepository<LeaveModel> leaveDal)
        {
            LeaveDal = leaveDal;
        }

        public async Task<ApiResult<int>> Handle(LeaveCommon request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();

            var info = LeaveDal.GetAll().Where(x => x.LeaveId == request.leaveId).FirstOrDefault();
            if (info == null)
            {
                res.Code = ApiEnum.Error;
                res.Message = "为查找到对应记录";
                return await Task.FromResult(res);
            }
            //if (request.Sates != 1 && request.Sates != 2)
            //{
            //    res.Code = ApiEnum.Error;
            //    res.Message = "审批状态无效";
            //    return await Task.FromResult(res);
            //}
            if (request.Sates == 2)
            {
                if (string.IsNullOrEmpty(request.leavecause))
                {
                    res.Code = ApiEnum.Error;
                    res.Message = "请填写不通过的具体原因";
                    return await Task.FromResult(res);
                }
                info.Sates = 2;
                info.leavecause = request.leavecause;
            }
            else if (request.Sates == 1)
            {
                info.Sates = 1;
                info.leavecause = null;
            }

            await LeaveDal.Update(info);

            res.Code = ApiEnum.Success;
            res.Message = "审批成功";
            return await Task.FromResult(res);
        }



    }
}
